/**
 * AmpArchPlugin.java
 * 
 * FreeZzaph is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * FreeZzaph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.
 */

package freezzaph.plugins;

import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import freezzaph.exceptions.PluginException;
import freezzaph.exceptions.URLFetchException;
import freezzaph.plugins.util.HTMLEntityDecoder;
import freezzaph.plugins.util.URLFetcher;

/**
 * FreeZzaph Plugin for the porn portal Ampland.com's archive.
 * 
 * @author FreeZzaph
 */
public final class AmpArchPlugin implements FreeZzaphDownloadPlugin {
	
	class AmpArchSettings {
		
		public void setYear(String year) {
			pluginSettings.put("year", year);
		}

		public String getYear() {
			return pluginSettings.get("year", null);
		}

		public void setMonth(String month) {
			pluginSettings.put("month", month);
		}

		public String getMonth() {
			return pluginSettings.get("month", null);
		}
	}
	
	private static final Pattern category = Pattern.compile("<a.+?href=\"http://www.amplandarchive.com/(\\w+).html\">([^<]+)</a>");
	
	private CategoryList pictures;
	private CategoryList movies;
	private HashMap<String, CategoryList> list;
	
	private AmpArchSettings settings = new AmpArchSettings();

	FreeZzaphPluginPreferences pluginSettings;

	/* (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphDownloadPlugin#getCategoryLists()
	 */
	public HashMap<String, CategoryList> getCategoryLists() {
		if (pictures == null || movies == null || list == null) {
			
			URL categoryListURL = null;
			try {
				categoryListURL = new URL("http://www.amplandarchive.com/mmpage.html");
			} catch (MalformedURLException e) {
				// Won't happen
			}
			
			// Download list with categories
			String data;
			try {
				data = URLFetcher.fetch(categoryListURL);
			} catch (URLFetchException e) {
				throw new PluginException("Could not download category list. Ampland.com may not be accessible, or the plugin may need to be updated.", e);
			}
			
			// Create category lists
			pictures = new CategoryList("pictures", "Picture galleries");
			movies = new CategoryList("movies", "Movie galleries");
			
			Matcher m = category.matcher(data);
			while (m.find()) {
				
				// Extract data from match
				String decodedName = HTMLEntityDecoder.decode(m.group(2));
				String name = Category.prettify(decodedName);
				String description = decodedName;
				
				String categoryName = m.group(1);
				
				// Create picture categories
				AmpArchPictureCategory ampPic = new AmpArchPictureCategory(name, description, categoryName, settings);
				pictures.addCategory(name, ampPic);
				
				// Create movie categories
				AmpArchMovieCategory ampMov = new AmpArchMovieCategory(name, description, categoryName, settings);
				movies.addCategory(name, ampMov);
			}

			// Create list over category lists
			list = new HashMap<String, CategoryList>(2);
			list.put("pictures", pictures);
			list.put("movies", movies);
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphDownloadPlugin#getPluginName()
	 */
	public String getPluginName() {
		return "amparch";
	}

	/* (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphDownloadPlugin#getPluginTitle()
	 */
	public String getPluginTitle() {
		return "Ampland Archive";
	}

	/*
	 * (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphDownloadPlugin#handleCommand(java.lang.String[])
	 */
	public boolean handleCommand(String[] command, PrintStream output) {
		if (command[0].equals("year")) {
			if (command.length == 1) {
				if (settings.getYear() != null) {
					output.printf("Year is set to: %s\n", settings.getYear());
				} else {
					output.printf("Year is unset\n");
				}
			}
			if (command.length == 2) {
				settings.setYear(command[1]);
			}
			return true;
		} else if (command[0].equals("month")) {
			if (command.length == 1) {
				if (settings.getMonth() != null) {
					output.printf("Month is set to: %s\n", settings.getMonth());
				} else {
					output.printf("Month is unset\n");
				}
			}
			if (command.length == 2) {
				settings.setMonth(command[1]);
			}
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphDownloadPlugin#showHelp()
	 */
	public String showHelp() {
		String help = "year -- See the current value of year\n";
		help += "year <year> -- Set the year value to 'year'\n";
		help += "month -- See the current value of month\n";
		help += "month <month> -- Set the year value to 'month'\n\n";
		
		help += "The year and month values decide from which year and month in the\n"
			 +	"archive we should gather URLs from. For year, enter the full year,\n"
			 +	"e.g. '2008', for month, enter the three-letter abbreviation, e.g.\n"
			 +	"'feb'. The legal range for this site is March 2004 through, well,\n"
			 +	"last month.";
		
		return help;
	}

	public void provideSettings(FreeZzaphPluginPreferences pluginSettingsObject) {
		pluginSettings = pluginSettingsObject;
	}

}
